<?php
namespace app\taobao\controller;
use think\Request;
use Curl\Curl;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/5
 * Time: 17:18
 */
class Spider{

    protected $productUrl = "http://item.taobao.com/item.htm?id=549608935104&abc=1";

    protected $productId = "";

    /**
     * 通过这个路径去访问商品的详情
     * @var string
     */
    protected $spiderUrl = [
        "http://hws.m.taobao.com/cache/wdetail/5.0/?id=#id#",
        'http://api.m.taobao.com/h5/mtop.taobao.detail.getdetail/6.0/?data=%7B%22itemNumId%22%3A%22#id#%22%7D&qq-pf-to=pcqq.c2c'
    ];


    //最终的数据 既可以用自己的软件上传 也可以用 淘宝助理上传 有免费的淘宝助理 用淘宝助理好了 这里组织数据就行了
    protected $result = [];


    public function index(){
        $this->getId();
        $response = $this->spider($this->spiderUrl[1]);
        $item = $response['data']['item'];
        $apiStack = $response['data']['apiStack'];
        $apiStack = json_decode($apiStack[0]['value'],true);
        $this->result['title'] = $item['title'];
        $this->result['cid'] = $item['categoryId'];
        $this->result['desc'] = $item['subtitle'];
        $this->result['price'] = $apiStack['price']['price']['priceText'];
        $this->result['num'] = $apiStack['skuCore']['sku2info'][0]['quantity'];
        $this->result['locationCity'] = $apiStack['delivery']['from'];//locationState 省 没有的话通过api去查询 没有办法了  补齐数据
        var_export($this->result);
    }

    private function spider($url){
        $url = str_replace("#id#",$this->productId,$url);
        $curl = new Curl();
        $curl->get($url);
        $response = json_decode($curl->response,true);
        var_export($response);
        return $response;
    }

    /**
     * 通过URl 获取 产品id
     * @return string
     */
    private function getId(){
        $result = parse_url($this->productUrl);
        $query = $result['query'];
        parse_str($query,$result);
        $this->productId = $result['id'];
        return $this->productId;
    }


    public function show(){
        $str = <<<CODE
{"layout":{},"delivery":{"from":"上海","to":"广东东莞","areaId":"441900","postage":"快递: 免运费","extras":{}},"item":{"couponUrl":"//h5.m.taobao.com/present/hongbao.html?sellerId=2448897384","showShopActivitySize":"2","sellCount":"7342","skuText":"请选择 颜色分类 尺码 "},"trade":{"buyEnable":"true","cartEnable":"true","buyParam":{},"cartParam":{},"hintBanner":{}},"feature":{"hasCoupon":"true","cainiaoNoramal":"true","hasSku":"true","showSku":"true","superActTime":"false"},"resource":{"share":{"name":"分享","iconType":"1"},"bigPromotion":{},"entrances":{},"coupon":{"couponList":[{"title":"5元店铺优惠券","icon":"//img.alicdn.com/tps/TB1kXgpNpXXXXaqXXXXXXXXXXXX-178-36.png"}],"h5LinkUrl":"//h5.m.taobao.com/jxt/coupon-tb.html"},"shopProm":[{"iconText":"满就送","content":["1.单笔订单满139元送礼物，免运费，送5M手机流量，亲，快下手吧！"]}]},"price":{"price":{"priceMoney":"14800","priceText":"148","type":"1"},"extraPrices":[{"priceMoney":"38900","priceText":"389","priceTitle":"价格","type":"2","lineThrough":"true"}],"priceTag":[{"text":"新品上新"},{"text":"淘金币抵2%","bgColor":"#ff9204"}],"shopProm":[{"title":"店铺优惠","content":["1.单笔订单满139元送礼物，免运费，送5M手机流量，亲，快下手吧！"]}]},"consumerProtection":{"items":[{"title":"8天退货","desc":"8天退货，退货邮费买家承担"},{"title":"运费险"},{"title":"公益宝贝"},{"title":"15天内发货"},{"title":"蚂蚁花呗"},{"title":"信用卡支付"},{"title":"集分宝"}],"passValue":"all"},"skuCore":{"sku2info":{"0":{"price":{"priceMoney":"14800","priceText":"148","priceTitle":"新品上新","type":"1"},"quantity":"11745"},"3528044915488":{"price":{"priceMoney":"14800","priceText":"148","type":"1"},"quantity":"867"},"3506517326872":{"price":{"priceMoney":"14800","priceText":"148","type":"1"},"quantity":"1259"},"3506517326873":{"price":{"priceMoney":"14800","priceText":"148","type":"1"},"quantity":"918"},"3506517326874":{"price":{"priceMoney":"14800","priceText":"148","type":"1"},"quantity":"1046"},"3528044915487":{"price":{"priceMoney":"14800","priceText":"148","type":"1"},"quantity":"1276"},"3506517326865":{"price":{"priceMoney":"14800","priceText":"148","type":"1"},"quantity":"1121"},"3506517326866":{"price":{"priceMoney":"14800","priceText":"148","type":"1"},"quantity":"633"},"3506517326867":{"price":{"priceMoney":"14800","priceText":"148","type":"1"},"quantity":"579"},"3506517326868":{"price":{"priceMoney":"14800","priceText":"148","type":"1"},"quantity":"817"},"3506517326869":{"price":{"priceMoney":"14800","priceText":"148","type":"1"},"quantity":"1087"},"3506517326870":{"price":{"priceMoney":"14800","priceText":"148","type":"1"},"quantity":"1106"},"3506517326871":{"price":{"priceMoney":"14800","priceText":"148","type":"1"},"quantity":"1036"}},"skuItem":{"location":"广东东莞"}},"vertical":{"askAll":{"askText":"这款料子厚不厚？透气吗？","askIcon":"https://img.alicdn.com/tps/TB1tVU6PpXXXXXFaXXXXXXXXXXX-102-60.png","linkUrl":"//h5.m.taobao.com/wendajia/question.htm?wdjType=1&spm=w-a2141.7631564&itemId=549608935104","title":"问大家(82)","questNum":"82","showNum":"2","modelList":[{"askText":"这款料子厚不厚？透气吗？","answerCountText":"8个回答"},{"askText":"质量好吗","answerCountText":"7个回答"}]}},"params":{"trackParams":{"layoutId":null}},"skuVertical":{}}
CODE;

        $res = json_decode($str,true);
        var_export($res);
    }


    //**获取类目
    public function getProperty(){



        $url = "http://service.shuaishou.com/service.asmx";
        $param = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><UpdatePropertyBySysSortId xmlns="http://www.cogentsoft.com.cn/"><loginSequence>12070283-6ba7-4da6-88ac-89e70b69d3d6</loginSequence><sysSortId>58831</sysSortId><lastUpdateTime>2017-04-19 11:51:08</lastUpdateTime></UpdatePropertyBySysSortId></soap:Body></soap:Envelope>';

        $param = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><DownloadPropertyInfoBySysSortId xmlns="http://www.cogentsoft.com.cn/"><loginSequence>12070283-6ba7-4da6-88ac-89e70b69d3d6</loginSequence><sysSortId>58831</sysSortId></DownloadPropertyInfoBySysSortId></soap:Body></soap:Envelope>';


        $SoapAction = "http://www.cogentsoft.com.cn/UpdatePropertyBySysSortId";

        $SoapAction = "http://www.cogentsoft.com.cn/DownloadPropertyInfoBySysSortId";
        $ch = curl_init (); //initiate the curl session
        curl_setopt ( $ch, CURLOPT_URL, $url ); //set to url to post to
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); // return data in a variable
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $param ); // post the xml
        curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 ); // set timeout in seconds
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
        $header = array ("Content-Type: text/xml" );
        $header [] = "Content-Length: ".strlen($param);

        $header [] = 'SOAPAction: "' . $SoapAction . '"';
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
        $xmlResponse = curl_exec ( $ch );
        curl_close ( $ch );
        echo $xmlResponse;


    }

}
